//  用于封装常用方法和公共业务
import { Models } from '../../common/typings/modle'
import pool from './pool'
import { DataBaseFailed } from '../../core/HttpException'

import { lineToHumpObject } from '../../common/utils/utls'

import mysql from 'mysql'

export async function command(command: string, value?: Array<any>): Promise<Models.Result> {
  try {
    return new Promise<Models.Result>((resolve, reject: (error: Models.MysqlError) => void) => {
      pool.getConnection((error: mysql.MysqlError, connection: mysql.PoolConnection) => {
        // 如果连接出错 抛出错误
        if (error) {
          const reuslt: Models.MysqlError = {
            error,
            msg: '数据库连接出错:' + ' ' + error.message,
          }
          reject(reuslt)
        }

        const callback: mysql.queryCallback = (err, results?: any, fields?: mysql.FieldInfo[]) => {
          connection.release()
          if (err) {
            const result: Models.MysqlError = {
              error: err,
              msg: err.sqlMessage || '数据库增删改查出错',
            }
            reject(result)
          } else {
            const result: Models.Result = {
              msg: 'ok',
              state: 1,
              // 将数据库得字段,有下划线改为驼峰
              results: results instanceof Array ? results.map(lineToHumpObject) : results,
              fields: fields || [],
            }
            resolve(result)
          }
        }
        // 执行mysql语句
        if (value) {
          pool.query(command, value, callback)
        } else {
          pool.query(command, callback)
        }
      })
    }).catch((error) => {
      throw new DataBaseFailed(error.msg)
    })
  } catch (error) {
    throw new DataBaseFailed()
  }
}
